package com.water.camera.fragment;

import android.icu.util.Calendar;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.github.gzuliyujiang.wheelpicker.entity.DateEntity;
import com.water.camera.databinding.DialogWatermarkTimeSelectBinding;

public class EditTimeDlg extends FullScreenDlg {
    DialogWatermarkTimeSelectBinding binding;

    @Override
    protected int getHeight() {
        return ViewGroup.LayoutParams.WRAP_CONTENT;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DialogWatermarkTimeSelectBinding.inflate(inflater, container, false);
        DateEntity today = DateEntity.today();
        binding.dateWheel.setRange(DateEntity.yearOnFuture(-5), null, today);
        binding.today.setText(today.getYear() + "年" + today.getMonth() + "月" + today.getDay() + "日");
        binding.cancel.setOnClickListener(v -> dismiss());
        binding.confirm.setOnClickListener(v -> {
            int y = binding.dateWheel.getSelectedYear();
            int m = binding.dateWheel.getSelectedMonth();
            int d = binding.dateWheel.getSelectedDay();
            int h = binding.timeWheel.getSelectedHour();
            int min = binding.timeWheel.getSelectedMinute();
            int s = binding.timeWheel.getSelectedSecond();

            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, y);
            c.set(Calendar.MONTH, m - 1);
            c.set(Calendar.DAY_OF_MONTH, d);
            c.set(Calendar.HOUR_OF_DAY, h);
            c.set(Calendar.MINUTE, min);
            c.set(Calendar.SECOND, s);
            long time = c.getTimeInMillis();
            dismiss(true, time);
        });
        return binding.getRoot();
    }
}
